package com.mengtian_tech.tju.dataquery.api.util;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class SessionUtil {
	private static Log  logger = LogFactory.getLog(SessionUtil.class);
	
	
	private static final String currentUser = "current_user_name";
	private static final String currentEmployee = "current_employee";
	private static final String callbackURL = "call_backurl";
	
	public static String getUser() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		return (String)session.getAttribute(currentUser);
	}

	public static void setUser(String staffId) {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		session.setAttribute(currentUser, staffId);
	}

	  

	public static void setSessionId( String sid) {

		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		session.setAttribute("sid", sid);
	}

	public static String getSessionId() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		return (String) session.getAttribute("sid");
	}
	
	public static void setCallbackURL (String url) {

		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		
		logger.debug("set callback url="+url+",session id="+session.getId());
		
		session.setAttribute(callbackURL, url);
	}
	
	/**
	 * 获取cas登录成功后的回调地址
	 */
	public static String getCallbackURL () {

		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		
		
		
		String url =  (String) session.getAttribute(callbackURL);
		
		logger.debug("get callback url="+url+",session id="+session.getId());
		
		return url;
	}

	public static void setClientType(Integer clientMobile) {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		session.setAttribute("client_type_01", clientMobile);
	}
	
	public static Integer getClientType() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = attrs.getRequest().getSession(); // 获取当前会话
		return (Integer) session.getAttribute("client_type_01");
	}
}
